import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home/mould',
    name: 'Home',
    meta: {
      requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
    },
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta: {
      requireAuth: true,  
      title:'应用工具'
    },
    children:[
      {
        path: '/home/mould',
        component:  () => import('@/views/Mould/index.vue'),
        name:'Mould',
        meta: {
          requireAuth: true,  
          title: '塑造管理',
          path: '/home/mould',
        },
        children:[{
          path: '/home/mould/mouldadd',
          component:  () => import('@/views/Mould/MouldAdd/index.vue'),
          name:'MouldAdd',
          meta: {
            requireAuth: true,  
            title: '新建塑造',
            path: '/home/mould/mouldadd',
          },
        },{
          path: '/home/mould/mouldetails',
          component:  () => import('@/views/Mould/MouldDetails/index.vue'),
          name:'MouldDetails',
          meta: {
            requireAuth: true,  
            title: '塑造报告',
            path: '/home/mould/mouldetails',
          },
        }]
      },
      {
        path: '/home/evidence',
        component:  () => import('@/views/Evidence/index.vue'),
        name:'Index',
        meta: {
          requireAuth: true, 
          title: '取证管理',
          path: '/home/evidence',
        },
        children:[{
          path: '/home/evidence/EvidenceAdd',
          component:  () => import('@/views/Evidence/EvidenceAdd/index.vue'),
          name:'MouldAdd',
          meta: {
            requireAuth: true,  
            title: '新建取证',
            path: '/home/evidence/EvidenceAdd',
          },
        },{
          path: '/home/evidence/EvidenceDetails',
          component:  () => import('@/views/Evidence/EvidenceDetails/index.vue'),
          name:'MouldDetails',
          meta: {
            requireAuth: true,  
            title: '取证报告',
            path: '/home/evidence/EvidenceDetails',
          },
        }]
      },{
        path: '/home/procedure',
        component:  () => import('@/views/Procedure/index.vue'),
        name:'Procedure',
        meta: {
          requireAuth: true, 
          title: '程序管理',
          path: '/home/procedure',
        },
        children:[{
          path: '/home/procedure/procedureadd',
          component:  () => import('@/views/Procedure/ProcedureAdd/index.vue'),
          name:'ProcedureAdd',
          meta: {
            requireAuth: true,  
            title: '新建程序',
            path: '/home/procedure/procedureadd',
          },
        },{
          path: '/home/procedure/procedureedit',
          component:  () => import('@/views/Procedure/ProcedureEdit/index.vue'),
          name:'ProcedureEdit',
          meta: {
            requireAuth: true,  
            title: '编辑程序',
            path: '/home/procedure/procedureedit',
          },
        },{
          path: '/home/procedure/procedurelook',
          component:  () => import('@/views/Procedure/ProcedureLook/index.vue'),
          name:'ProcedureLook',
          meta: {
            requireAuth: true,  
            title: '查看程序',
            path: '/home/procedure/procedurelook',
          },
        }]
      },
      {
        path: '/home/multimedia',
        component:  () => import('@/views/Multimedia/index.vue'),
        name:'Multimedia',
        meta: {
          requireAuth: true, 
          title: '多媒体管理',
          path: '/home/multimedia',
        },
      },
      {
        path: '/home/evaluate',
        component:  () => import('@/views/Evaluate/index.vue'),
        name:'evaluate',
        meta: {
          requireAuth: true, 
          title: '评价管理',
          path: '/home/evaluate',
        },
        children:[{
          path: '/home/evaluate/evaluateadd',
          component:  () => import('@/views/Evaluate/EvaluateAdd/index.vue'),
          name:'EvaluateAdd',
          meta: {
            requireAuth: true,  
            title: '塑造模型评价',
            path: '/home/evaluate/evaluateadd',
          },
        }]
      },
      {
        path: '/home/user',
        component:  () => import('@/views/User/index.vue'),
        name:'evaluate',
        meta: {
          requireAuth: true, 
          title: '用户管理',
          path: '/home/user',
        },
        children:[{
          path: '/home/user/useradd',
          component:  () => import('@/views/User/UserAdd/index.vue'),
          name:'UserAdd',
          meta: {
            requireAuth: true,  
            title: '新建用户',
            path: '/home/user/useradd',
          },
        },{
          path: '/home/user/useredit',
          component:  () => import('@/views/User/UserEdit/index.vue'),
          name:'EvaluateEdit',
          meta: {
            requireAuth: true,  
            title: '编辑用户',
            path: '/home/user/useredit',
          },
        }]
      },
      {
        path: '/home/test',
        component:  () => import('@/views/test/index.vue'),
        name:'Test',
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login/index.vue')
  },
  {
    path: '/404',
    name: '404',
    component: () => import('../views/404.vue')
  },
  {
    path: '*',
    redirect: '/404'
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {   //登录权鉴
  let tokens = sessionStorage.getItem('token')
  if (to.path == '/login' && !tokens) {
    next();
  } else if (to.path == '/login' && tokens) {
    next({
      path: '/home'
    });
  } else {
    if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
      if (tokens) {
        next()
      } else {
        next({
          path: '/login',
        })
      }

    } else {
      next();
    }
  }
})


export default router
